const SegmentTree = require("../template")

function testSegmentTreePerformance() {
  console.log(
    "1. 大量数据构建性能测试:",
    (() => {
      const dataSize = 1000
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      
      const start = performance.now()
      const st = new SegmentTree(arr)
      const end = performance.now()
      
      const buildTime = end - start
      
      return buildTime < 1000 // 1秒内完成构建
    })()
  )

  console.log(
    "2. 大量区间查询性能测试:",
    (() => {
      const dataSize = 1000
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      const st = new SegmentTree(arr)
      
      const start = performance.now()
      
      // 执行大量区间查询
      for (let i = 0; i < 100; i++) {
        const l = Math.floor(Math.random() * dataSize)
        const r = l + Math.floor(Math.random() * (dataSize - l))
        st.getRangeSum(l, r)
      }
      
      const end = performance.now()
      const queryTime = end - start
      
      return queryTime < 1000 // 1秒内完成查询
    })()
  )

  console.log(
    "3. 大量区间更新性能测试:",
    (() => {
      const dataSize = 1000
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      const st = new SegmentTree(arr)
      
      const start = performance.now()
      
      // 执行大量区间更新
      for (let i = 0; i < 100; i++) {
        const l = Math.floor(Math.random() * dataSize)
        const r = l + Math.floor(Math.random() * (dataSize - l))
        const val = Math.floor(Math.random() * 100)
        st.updateRangeValue(l, r, val)
      }
      
      const end = performance.now()
      const updateTime = end - start
      
      return updateTime < 1000 // 1秒内完成更新
    })()
  )

  console.log(
    "4. 大量单点更新性能测试:",
    (() => {
      const dataSize = 1000
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      const st = new SegmentTree(arr)
      
      const start = performance.now()
      
      // 执行大量单点更新
      for (let i = 0; i < 1000; i++) {
        const idx = Math.floor(Math.random() * dataSize)
        const val = Math.floor(Math.random() * 100)
        st.updateSingleValue(idx, val)
      }
      
      const end = performance.now()
      const updateTime = end - start
      
      return updateTime < 1000 // 1秒内完成更新
    })()
  )

  console.log(
    "5. 混合操作性能测试:",
    (() => {
      const dataSize = 1000
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      const st = new SegmentTree(arr)
      
      const start = performance.now()
      
      // 混合操作
      for (let i = 0; i < 100; i++) {
        const operation = Math.floor(Math.random() * 3)
        
        if (operation === 0) {
          // 区间查询
          const l = Math.floor(Math.random() * dataSize)
          const r = l + Math.floor(Math.random() * (dataSize - l))
          st.getRangeSum(l, r)
        } else if (operation === 1) {
          // 区间更新
          const l = Math.floor(Math.random() * dataSize)
          const r = l + Math.floor(Math.random() * (dataSize - l))
          const val = Math.floor(Math.random() * 100)
          st.updateRangeValue(l, r, val)
        } else {
          // 单点更新
          const idx = Math.floor(Math.random() * dataSize)
          const val = Math.floor(Math.random() * 100)
          st.updateSingleValue(idx, val)
        }
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000 // 1秒内完成混合操作
    })()
  )

  console.log(
    "6. 线段树内存效率测试:",
    (() => {
      const dataSize = 1000
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      
      const start = performance.now()
      const st = new SegmentTree(arr)
      const end = performance.now()
      
      const buildTime = end - start
      
      // 验证内存使用合理
      return buildTime < 1000 &&
             st !== null &&
             st !== undefined
    })()
  )

  console.log(
    "7. 线段树并发操作性能测试:",
    (() => {
      const dataSize = 1000
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      const st = new SegmentTree(arr)
      
      const start = performance.now()
      
      // 并发操作
      for (let i = 0; i < 100; i++) {
        // 同时进行查询和更新
        const l = Math.floor(Math.random() * dataSize)
        const r = l + Math.floor(Math.random() * (dataSize - l))
        
        st.getRangeSum(l, r)
        st.updateRangeValue(l, r, i)
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000
    })()
  )

  console.log(
    "8. 线段树批量操作性能测试:",
    (() => {
      const dataSize = 1000
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      const st = new SegmentTree(arr)
      
      const start = performance.now()
      
      // 批量操作
      const batchSize = 100
      for (let batch = 0; batch < 10; batch++) {
        for (let i = 0; i < batchSize; i++) {
          const idx = batch * batchSize + i
          st.updateSingleValue(idx, batch)
        }
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000
    })()
  )

  console.log(
    "9. 线段树范围查询性能测试:",
    (() => {
      const dataSize = 1000
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      const st = new SegmentTree(arr)
      
      const start = performance.now()
      
      // 范围查询测试
      const ranges = [
        [0, 100],
        [100, 200],
        [200, 300],
        [300, 400],
        [400, 500]
      ]
      
      ranges.forEach(range => {
        st.getRangeSum(range[0], range[1])
      })
      
      const end = performance.now()
      const queryTime = end - start
      
      return queryTime < 100
    })()
  )

  console.log(
    "10. 线段树单点查询性能测试:",
    (() => {
      const dataSize = 1000
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      const st = new SegmentTree(arr)
      
      const start = performance.now()
      
      // 单点查询测试
      for (let i = 0; i < 1000; i++) {
        const idx = Math.floor(Math.random() * dataSize)
        st.getSingleValue(idx)
      }
      
      const end = performance.now()
      const queryTime = end - start
      
      return queryTime < 1000
    })()
  )

  console.log(
    "11. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(arr) {
          const st = new SegmentTree(arr)
          const start = performance.now()
          
          st.getRangeSum(0, arr.length - 1)
          
          const end = performance.now()
          const time = end - start
          
          return {
            time: time,
            result: arr.length * this.multiplier
          }
        }
      }
      
      const arr = Array.from({ length: 100 }, (_, i) => i + 1)
      const result = perfObj.processPerformance(arr)
      
      return result.result === 200 &&
             result.time < 100
    })()
  )

  console.log(
    "12. 线段树性能一致性测试:",
    (() => {
      const dataSize = 100
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      
      // 多次构建测试一致性
      const times = []
      for (let i = 0; i < 10; i++) {
        const start = performance.now()
        const st = new SegmentTree(arr)
        const end = performance.now()
        times.push(end - start)
      }
      
      // 验证性能一致性
      const avgTime = times.reduce((a, b) => a + b, 0) / times.length
      const maxTime = Math.max(...times)
      const minTime = Math.min(...times)
      
      return avgTime < 100 &&
             maxTime < 200 &&
             minTime >= 0
    })()
  )
}

testSegmentTreePerformance()
